package cn.itsource.org.service.impl;

import cn.itsource.basic.util.QueryResult;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.add(department);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        departmentMapper.remove(id);
    }

    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public Department loadOne(Long id) {
        return departmentMapper.loadOne(id);
    }

    @Override
    public List<Department> loadAll() {
        return departmentMapper.loadAll();
    }

    @Override
    public QueryResult<Department> queryPage(DepartmentQuery query) {
        Integer total = departmentMapper.queryCount(query);
        if (total == 0){
            return new QueryResult<>();
        }else {
            List<Department> departments = departmentMapper.queryList(query);
            return new QueryResult<>(total,departments);
        }
    }
}
